<template>
  <div>
    <PageTitle :select="2"></PageTitle>
    <div class="cont" :style="{ height: `${clientHeight / scaleX - 90}px` }">
      <el-scrollbar style="height: 95%">
        <!-- 左边 -->
        <!-- <div class="left">
          <div class="item">
            <div class="videoType1">
              <indexFlv width="100%" height="205px" :path="state.carHomeOptions[0].rtsp?.url"
                :name="state.carHomeOptions[0].rtsp?.name"></indexFlv>
            </div>
          </div>
          <div class="box">
            <indexFlv width="100%" height="205px" :path="state.carHomeOptions[1].rtsp?.url"
              :name="state.carHomeOptions[1].rtsp?.name"></indexFlv>
          </div>
          <div class="box">
            <indexFlv width="100%" height="205px" :path="state.carHomeOptions[2].rtsp?.url"
              :name="state.carHomeOptions[2].rtsp?.name"></indexFlv>
          </div>
          <div class="box">
            <indexFlv width="100%" height="205px" :path="state.carHomeOptions[3].rtsp?.url"
              :name="state.carHomeOptions[3].rtsp?.name"></indexFlv>
          </div>
        </div> -->

        <!-- 中间 -->
        <div class="center">
          <div class="top">
            <div class="time">
              {{ state.initTime.substring(0, 10) }}
              {{ state.initTime.substring(11, 19) }}
            </div>
            <div class="select-right-ls">
              <div class="select" style="margin-left: 100px">
                <el-select @change="selectArea" v-model="state.carHome" placeholder="Select" size="large"
                  style="width: 100%" :suffix-icon="IconDropDown">
                  <el-option v-for="item in state.carHomeOptions" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>
              </div>
              <div class="select sel-per-pol">
                <el-select @change="selectUrl" v-model="state.rtspValue" value-key="url" placeholder="Select"
                  size="large" style="width: 100%" :suffix-icon="IconDropDown">
                  <el-option v-for="item in state.rtspConfig" :key="item.name" :label="item.name" :value="item" />
                </el-select>
              </div>
            </div>
          </div>

          <div class="center_item">
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[0].rtsp?.url"
                :name="state.carHomeOptions[0].rtsp?.name"></indexFlv>
            </div>
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[1].rtsp?.url"
                :name="state.carHomeOptions[1].rtsp?.name"></indexFlv>
            </div>
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[2].rtsp?.url"
                :name="state.carHomeOptions[2].rtsp?.name"></indexFlv>
            </div>
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[3].rtsp?.url"
                :name="state.carHomeOptions[3].rtsp?.name"></indexFlv>
            </div>
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[4].rtsp?.url"
                :name="state.carHomeOptions[4].rtsp?.name"></indexFlv>
            </div>
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[5].rtsp?.url"
                :name="state.carHomeOptions[5].rtsp?.name"></indexFlv>
            </div>
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[6].rtsp?.url"
                :name="state.carHomeOptions[6].rtsp?.name"></indexFlv>
            </div>
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[7].rtsp?.url"
                :name="state.carHomeOptions[7].rtsp?.name"></indexFlv>
            </div>
            <div class="item">
              <indexFlv width="100%" height="280px" :path="state.carHomeOptions[8].rtsp?.url"
                :name="state.carHomeOptions[8].rtsp?.name"></indexFlv>
            </div>
          </div>
        </div>

        <!-- 右边 -->
        <!-- <div class="right">
          <div class="item">
            <div class="videoType2">
              <indexFlv width="100%" height="205px" :path="state.carHomeOptions[12].rtsp?.url"
                :name="state.carHomeOptions[12].rtsp?.name"></indexFlv>
            </div>
          </div>
          <div class="boxR">
            <indexFlv width="100%" height="205px" :path="state.carHomeOptions[13].rtsp?.url"
              :name="state.carHomeOptions[13].rtsp?.name"></indexFlv>
          </div>
          <div class="boxR">
            <indexFlv width="100%" height="205px" :path="state.carHomeOptions[14].rtsp?.url"
              :name="state.carHomeOptions[14].rtsp?.name"></indexFlv>
          </div>
          <div class="boxR">
            <indexFlv width="100%" height="205px" :path="state.carHomeOptions[15].rtsp?.url"
              :name="state.carHomeOptions[15].rtsp?.name"></indexFlv>
          </div>
        </div> -->
      </el-scrollbar>
    </div>
  </div>
</template>
<script setup lang="ts">
import moment from 'moment';
import { useBaseDataStore } from '@/store';
import IconDropDown from '@/components/IconDropDown.vue';
import indexFlv from "./cpns/index-flv.vue";
import { computed, onMounted, ref } from 'vue';
import { getBulkget, getRtspConfig } from "@/api/rtspapi";
/** @api */
const baseDataStore = useBaseDataStore();
/** @数据 */
const scaleX = computed(() => {
  return baseDataStore.getBaseData('scaleX') as number;
});
const clientHeight = computed(() => {
  return baseDataStore.getBaseData('clientHeight') as number;
});

interface State {
  /** 数据状态 */
  loading: boolean;
  maxTime: any;
  dateMin: any;
  /** 时间定时器 */
  timer?: any;
  getDataTimer?: any;
  /** 实时时间 */
  initTime: string;
  /** 时间范围 */
  timeArr: Array<string>;
  scrollTimer?: any;
  /** 当前选择的区域 */
  carHome: number;
  /** 区域列表 */
  carHomeOptions: { label: string, value: number, rtsp?: { name: string, url: string, pagePath?: number } }[];
  /** 当前选择的监控 */
  rtspValue: { name: string, url: string, pagePath?: number } | undefined;
  /** 监控列表 */
  rtspConfig: { name: string, url: string, pagePath?: number }[];
}

let state = ref<State>({
  loading: false,
  timeArr: [],
  maxTime: '',
  dateMin: 0,
  initTime: '',
  scrollTimer: undefined,
  carHome: 1,
  carHomeOptions: [
    { label: '区域一', value: 1, rtsp: undefined },
    { label: '区域二', value: 2, rtsp: undefined },
    { label: '区域三', value: 3, rtsp: undefined },
    { label: '区域四', value: 4, rtsp: undefined },
    { label: '区域五', value: 5, rtsp: undefined },
    { label: '区域六', value: 6, rtsp: undefined },
    { label: '区域七', value: 7, rtsp: undefined },
    { label: '区域八', value: 8, rtsp: undefined },
    { label: '区域九', value: 9, rtsp: undefined },
    { label: '区域十', value: 10, rtsp: undefined },
    { label: '区域十一', value: 11, rtsp: undefined },
    { label: '区域十二', value: 12, rtsp: undefined },
    { label: '区域十三', value: 13, rtsp: undefined },
    { label: '区域十四', value: 14, rtsp: undefined },
    { label: '区域十五', value: 15, rtsp: undefined },
    { label: '区域十六', value: 16, rtsp: undefined },
  ],
  rtspValue: undefined,
  rtspConfig: []
});
onMounted(() => {
  getInitTime();
  getRtspList();
});

/** @生命周期 */
/** @方法 */
function getInitTime() {
  state.value.timer = setInterval(() => {
    const initTime = moment().format('YYYY-MM-DD HH:mm:ss');
    /** 判断是否跨天 */
    if (
      state.value.initTime &&
      state.value.initTime.substring(0, 10) != initTime.substring(0, 10)
    ) {
      /** 重新获取一次时间 */
      updateDate();
      setRegular(true);
    }
    state.value.initTime = initTime;
  }, 1000);
}
/** 定时刷新  操作页面一次重新计时 isRefresh:是否重置定时器*/
function setRegular(isRefresh: boolean = false) {
  if (state.value.getDataTimer && isRefresh) {
    state.value.getDataTimer = null;
  }
  state.value.getDataTimer = setInterval(() => { }, 1000 * 60 * 1);
}
/** 获取时间间隔 */
function updateDate() {
  /** 当前时区时间 */
  const Min = moment().subtract('days', 14).format('YYYY-MM-DD');
  const Max = moment().format('YYYY-MM-DD');
  state.value.timeArr = [Min, Max];
}
/** 获取监控列表 */
function getRtspList() {
  getRtspConfig().then(res => {
    let list = res.data.rtspList;
    // state.value.rtspConfig = list;
    // for (let i = 1; i <= 16; i++) {
    //   const values = list.find(item => item.pagePath === i);
    //   state.value.carHomeOptions[i - 1].rtsp = values;
    //   if (i === 1) state.value.rtspValue = values;
    // }
    const data = list.map(item => item.url);
    getBulkget(data).then(({ data }) => {
      list.forEach(item => { item.url = (data as any)[item.url] });
      state.value.rtspConfig = list;
      for (let i = 1; i <= 16; i++) {
        const values = list.find(item => item.pagePath === i);
        state.value.carHomeOptions[i - 1].rtsp = values;
        if (i === 1) state.value.rtspValue = values;
      }
    })
  })
}
/** 选择区域 */
function selectArea(value: number) {
  const selectValue = state.value.rtspConfig.find(item => item.pagePath === value);
  state.value.rtspValue = selectValue;
}
/** 选择监控地址 */
interface Info {
  name: string;
  url: string;
  pagePath?: number | undefined;
}
function selectUrl(info: Info) {
  const { rtspConfig, carHome, carHomeOptions } = state.value;
  const oldInfo = rtspConfig.find(item => item.pagePath === carHome) as Info;//该区域以前的rtsp
  const num1 = info.pagePath;
  const num2 = oldInfo.pagePath;
  info.pagePath = num2;
  oldInfo.pagePath = num1;
  if (num1) carHomeOptions[num1 - 1].rtsp = oldInfo;
  if (num2) carHomeOptions[num2 - 1].rtsp = info;
}
/** @侦听器 */
/** @公共方法 */
</script>
<style lang="scss" scoped>
// .flex {
//   display: flex;
// }

.cont {
  padding: 50px 8px 10px;
  background: url('@/assets/imgs/bg-03.png') no-repeat center;
  background-size: 100% 100%;
  margin: -40px 5px 0;

  // .left {
  //   width: 33.3vw;
  //   flex: 1;
  //   // width: 444.25px;
  //   height: 100%;

  //   // background-color: red;
  //   .item {
  //     height: 228px;
  //     box-sizing: border-box;
  //     // padding: 10px;
  //     background: url(../../assets/imgs/containerL.png) no-repeat center center;
  //     background-size: 100% 100%;
  //     margin-bottom: 10px;
  //     overflow: hidden;
  //   }

  //   .box {
  //     height: 228px;
  //     box-sizing: border-box;
  //     padding: 10px;
  //     background: url(../../assets/imgs/boxq.png) no-repeat center center;
  //     background-size: 100% 100%;
  //     margin-bottom: 10px;
  //   }
  // }

  .center {
    width: 100%;
    // width: 912.5px;
    height: 100%;
    margin: 0 24.75px;

    .select-right-ls {
      display: flex;

      .sel-per-pol {
        margin-left: 25px;
      }
    }

    .top {
      display: flex;
      // justify-content: space-between;
      justify-content: center;
      height: 44px;

      .time {
        color: #ffffff;
        font-family: Microsoft YaHei;
        font-size: 24px;
        font-weight: normal;
        margin-right: 250px;
        width: 252px;
      }

      .select {
        // width: 220px;
        // height: 30px;
        width: 167px;
        height: 47px;
      }
    }

    .center_item {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: space-between;

      .item {
        // width: 445px;
        width: 33%;
        height: 300px;
        box-sizing: border-box;
        padding: 10px;
        background: url(../../assets/imgs/box-01.png) no-repeat center center;
        // background: url(../../assets/imgs/boxq.png) no-repeat center center;
        background-size: 100% 100%;
        margin-bottom: 15px;
      }
    }
  }

  // .right {
  //   width: 33.3vw;
  //   flex: 1;
  //   // width: 446px;
  //   height: 100%;

  //   // background-color: red;
  //   .item {
  //     height: 228px;
  //     background: url(../../assets/imgs/containerR.png) no-repeat center center;
  //     margin-bottom: 10px;
  //   }

  //   .boxR {
  //     height: 228px;
  //     box-sizing: border-box;
  //     padding: 10px;
  //     background: url(../../assets/imgs/boxR.png) no-repeat center center;
  //     margin-bottom: 10px;
  //   }
  // }
}

.videoType1 {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 10px;
  border-top-right-radius: 100px;
  overflow: hidden;
}

.videoType2 {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 10px;
  border-top-left-radius: 100px;
  overflow: hidden;
}

:deep(.el-scrollbar__view) {
  height: 100%;
  display: flex;
}

:deep(.el-input__wrapper) {
  background-color: #393939;
  box-shadow: none !important;
  color: #fff !important;
  height: 32px !important;
}

*:deep(.el-input__inner) {
  color: #fff !important;
}

:deep(.el-input__suffix-inner) {
  align-items: baseline !important;
}

:deep(.el-select-dropdown__wrap) {
  height: 100px !important;
}

:deep(.el-select-dropdown) {
  width: 110px !important;
}

:deep(.el-select-dropdown__item.selected) {
  color: red !important;
}
</style>
<style lang="scss">
:deep(.el-popper) {
  .is-light {
    background-color: #092d66 !important;
    color: white !important;
  }
}

:deep(.el-select__popper) {
  background-color: #092d66 !important;
  color: white !important;
}
</style>
